package com.leo.basic.utils;

import com.leo.basic.scheduler.bean.SchedulerJob;
import com.leo.basic.scheduler.constant.SchedulerConstant;
import com.leo.basic.scheduler.quartz.BaseJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @ClassName TashUtils
 * @Description 通过反射执行schedulerJob中定义的方法
 * @Author wangss
 * @date 2021.09.23 17:14
 * @Version 1.0
 */
@Slf4j
public class TaskUtils {

    public static void invokeMethod(SchedulerJob schedulerJob) throws Exception {
        BaseJob baseJob = null;
        Class clazz = null;
        if (StringUtils.isNoneBlank(schedulerJob.getSpringId())) {
            baseJob = (BaseJob) SpringContextUtil.getBean(schedulerJob.getSpringId());
        } else if (StringUtils.isNotBlank(schedulerJob.getBeanClass())) {
            try {
                clazz = Class.forName(schedulerJob.getJobName());
                if (BaseJob.class.isAssignableFrom(clazz)) {
                    baseJob = (BaseJob) clazz.newInstance();
                }
            } catch (Exception e) {
                log.error("获取BaseJob异常：{}", e.getMessage());
            }
        }
        if (null == baseJob) {
            log.error("任务名称 = [{}] ---------------- 未启动成功0，请检查配置是否正确！！！", schedulerJob.getJobName());
            throw new Exception("任务名称 = [" + schedulerJob.getJobName() + "] ---------------- 未启动成功0，请检查配置是否正确！！！");
        }

        Method method = null;
        try {
            if (StringUtils.isNotEmpty(schedulerJob.getParameterJson())) {
                String[] getParams = schedulerJob.getParameterJson().replaceAll("\r|\n", "").split(";");
                HashMap<String, String> parameterMap = new HashMap<>();
                String spitParamKey = "";
                String spitParamValue = "";
                for (String param : getParams) {
                    spitParamKey = param;
                    spitParamValue = param;
                    int idx = spitParamKey.indexOf('=');
                    spitParamKey = spitParamKey.substring(0, idx);
                    int idx1 = spitParamValue.indexOf('=');
                    spitParamValue = spitParamValue.substring(idx1 + 1, spitParamValue.length());
                    parameterMap.put(spitParamKey.trim(), spitParamValue.trim());
                }
                ReflectUtil.setFieldValue(baseJob, SchedulerConstant.JOB_MAP_KEY, parameterMap);
            }
            if (null != clazz) {
                method = clazz.getDeclaredMethod(schedulerJob.getMethodName());
            }
        } catch (Exception e) {
            log.error("任务名称 = [{}] --------------未启动成功1，方法名设置错误！！！", schedulerJob.getJobName());
            log.error(e.getMessage());
            throw new Exception("任务名称 = [" + schedulerJob.getJobName() + "] --------------未启动成功1，方法名设置错误！！！");
        }

        if (null != method) {
            try {
                method.invoke(baseJob);
            } catch (Exception e) {
                log.error("任务名称 = [{}] --------------未启动成功2，方法名设置错误！！！", schedulerJob.getJobName());
                log.error(e.getMessage());
                throw new Exception("任务名称 = [" + schedulerJob.getJobName() + "] --------------未启动成功2，方法名设置错误！！！");
            }
        }
        log.info("任务名称 = [{}] --------------启动成功！！！", schedulerJob.getJobName());
    }
}
